package com.nanyang.tms.common.util;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Pattern;

/**
 * 
 * <p>
 * CheckUtils
 * </p>
 * <p>
 * Description:(校验工具类)
 * </p>
 * 
 * @author jiabin
 * @date 2020年11月27日 上午8:20:38
 * @Copyright Copyright (c) 2020 www.rongetong.cn Inc. All rights reserved.
 */
public class CheckUtils {

	/**
	 * 
	 * <p>
	 * checkLength
	 * </p>
	 * <p>
	 * Description:(校验字符串长度)
	 * </p>
	 * 
	 * @param str
	 * @param length
	 * @return
	 */
	public static Boolean checkLength(String str, int length) {
		if (str.length() > length) {
			return false;
		}
		return true;
	}

	/**
	 * 
	 * <p>
	 * checkLength1
	 * </p>
	 * <p>
	 * Description:(校验字符串长度)
	 * </p>
	 * 
	 * @param str
	 * @param length
	 * @return
	 */
	public static Boolean checkLength1(String str, int length) {
		if (str.length() != length) {
			return false;
		}
		return true;
	}

	/**
	 * 
	 * <p>
	 * checkChineseCharacters
	 * </p>
	 * <p>
	 * Description:(校验汉字)
	 * </p>
	 * 
	 * @param str
	 * @return
	 */
	public static Boolean checkChineseCharacters(String str) {
		String pattern = "^[\\u4e00-\\u9fa5()（）\\·—\\-－&]+$";
		return Pattern.matches(pattern, str);
	}

	/**
	 * <p>
	 * checkNumber
	 * </p>
	 * <p>
	 * Description:校验银行账号 -- 只能为数字
	 * </p>
	 * 
	 * @param str
	 * @return
	 */
	public static Boolean checkNumber(String str) {
		String pattern = "^\\d+$";
		return Pattern.matches(pattern, str);
	}

	/**
	 * <p>
	 * checkTaxNoValue
	 * </p>
	 * <p>
	 * Description:校验纳税人识别号格式是否正确
	 * </p>
	 * 
	 * @param str
	 * @return
	 */
	public static Boolean checkTaxNoValue(String str) {
		String pattern = "^[0-9A-Z]{15,20}$";
		return Pattern.matches(pattern, str);
	}

	/**
	 * <p>
	 * checkAmount
	 * </p>
	 * <p>
	 * Description:校验金额
	 * </p>
	 * 
	 * @param str
	 * @return
	 */
	public static Boolean checkAmount(String str) {
		String pattern = "^[+\\-]?([0-9]+(.[0-9]{1,6})?)$";
		return Pattern.matches(pattern, str);
	}

	/**
	 * <p>
	 * checkEmailValue
	 * </p>
	 * <p>
	 * Description:校验邮箱格式是否正确
	 * </p>
	 * 
	 * @param str
	 * @return
	 */
	public static Boolean checkEmailValue(String str) {
		String pattern = "^[0-9a-zA-Z_\\.\\-]+@(([0-9a-zA-Z\\-]+)[.]){1,2}[a-zA-Z]{2,3}$";
		return Pattern.matches(pattern, str);
	}

	/**
	 * <p>
	 * checkMobileValue
	 * </p>
	 * <p>
	 * Description:校验手机号格式是否正确
	 * </p>
	 * 
	 * @param str
	 * @return
	 */
	public static Boolean checkMobileValue(String str) {
		String pattern = "^1\\d{10}$";
		return Pattern.matches(pattern, str);
	}

	/**
	 * <p>
	 * checkEnglish
	 * </p>
	 * <p>
	 * Description:校验英文
	 * </p>
	 * 
	 * @param str
	 * @return
	 */
	public static Boolean checkEnglish(String str) {
		String pattern = "^[A-Za-z]*$";
		return Pattern.matches(pattern, str);
	}

	/**
	 * <p>
	 * checkDateTime
	 * </p>
	 * <p>
	 * Description:校验日期+时间格式
	 * </p>
	 * 
	 * @param str
	 * @return
	 */
	public static Boolean checkDateTime(String str) {
		String pattern = "^[1-9]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\\s+(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d$";
		return Pattern.matches(pattern, str);
	}

	/**
	 * <p>
	 * checkDate
	 * </p>
	 * <p>
	 * Description:校验日期格式
	 * </p>
	 * 
	 * @param str
	 * @return
	 */
	public static Boolean checkDate(String str) {
		String pattern = "^[1-9]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$";
		return Pattern.matches(pattern, str);
	}

	/**
	 * <p>
	 * checkTime
	 * </p>
	 * <p>
	 * Description:校验时间格式
	 * </p>
	 * 
	 * @param str
	 * @return
	 */
	public static Boolean checkTime(String str) {
		String pattern = "^(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d$";
		return Pattern.matches(pattern, str);
	}

	/**
	 * <p>
	 * checkSwiftCode
	 * </p>
	 * <p>
	 * Description:校验SwiftCode
	 * </p>
	 * 
	 * @param str
	 * @return
	 */
	public static Boolean checkSwiftCode(String str) {
		String pattern = "^[A-Z0-9]{11}$";
		return Pattern.matches(pattern, str);
	}




	/**
	 * <p>
	 * checkCrossEnglish
	 * </p>
	 * <p>
	 * Description:校验跨境英文
	 * </p>
	 * 
	 * @param str
	 * @return
	 */
	public static Boolean checkCrossEnglish(String str) {
		String pattern = "^[0-9a-zA-Z/?(){}（），+-:.,'&_ ]+$";
		return Pattern.matches(pattern, str);
	}


	/**
	 * 
	 * <p>
	 * checkSellerCompanyName
	 * </p>
	 * <p>
	 * Description:(校验收款企业名称)
	 * </p>
	 * 
	 * @param str
	 * @return
	 */
	public static Boolean checkSellerCompanyName(String str) {
		String pattern = "^[\\u4e00-\\u9fa5（）—0-9a-zA-Z/?(){}+-:.,' ]+$";
		return Pattern.matches(pattern, str);
	}

	/**
	 * <p>
	 * checkInvoiceNo
	 * </p>
	 * <p>
	 * Description:校验发票号
	 * </p>
	 * 
	 * @param str
	 * @return
	 */
	public static Boolean checkInvoiceNo(String str) {
		String pattern = "^[0-9a-zA-Z]+$";
		return Pattern.matches(pattern, str);
	}
	
	/**
	 * <p>
	 * checkEnNum
	 * </p>
	 * <p>
	 * Description:校验是否为数字或英文大小写
	 * </p>
	 * 
	 * @param str
	 * @return
	 */
	public static Boolean checkEnNum(String str) {
		String pattern = "^[0-9a-zA-Z]+$";
		return Pattern.matches(pattern, str);
	}

	/**
	 * 
	 * <p>
	 * checkCompanyName
	 * </p>
	 * <p>
	 * Description:(校验企业名称是否一致（忽略全角半角括号及空格）)
	 * </p>
	 * 
	 * @param
	 * @return
	 */
	public static boolean checkCompanyName(String companyName1, String companyName2) {

		if (StringUtils.isEmpty(companyName1) || StringUtils.isEmpty(companyName2)) {
			return false;
		}

		return StringUtils.equals(
				companyName1.replaceAll("（", "(").replaceAll("）", ")").replaceAll("－", "-").replaceAll("—", "-").replaceAll(" ", "").replaceAll("　", ""),
				companyName2.replaceAll("（", "(").replaceAll("）", ")").replaceAll("－", "-").replaceAll("—", "-").replaceAll(" ", "").replaceAll("　", ""));

	}

	/**
	 * <p>
	 * checkBankAccount
	 * </p>
	 * <p>
	 * Description:(校验票面上的银行信息)
	 * </p>
	 * 
	 * @param invoiceBankInfo
	 *            发票上的银行信息
	 * @param orderBankAccount
	 *            订单上的银行账号
	 * @return
	 */
	public static boolean checkBankAccount(String invoiceBankInfo, String orderBankAccount) {

		if (StringUtils.isEmpty(invoiceBankInfo) || StringUtils.isEmpty(orderBankAccount)) {
			return false;
		}

		// 替换特殊字符及全角数字
		String formatBankInfo = invoiceBankInfo.replaceAll("-", "").replaceAll("—", "").replaceAll("\\?", "")
				.replaceAll("－", "-").replaceAll("_", "").replaceAll(" ", "").replaceAll("１", "1").replaceAll("２", "2")
				.replaceAll("３", "3").replaceAll("４", "4").replaceAll("５", "5").replaceAll("６", "6")
				.replaceAll("７", "7").replaceAll("８", "8").replaceAll("９", "9").replaceAll("０", "0");

		return formatBankInfo.contains(orderBankAccount);

	}

}
